package cn.coders.haoniu.caipiao.dao;

import javax.persistence.EntityManagerFactory;

import org.hibernate.SessionFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
public class HibernateAutoConfiguration {

    /*
     * @Bean public SessionFactory sessionFactory(HibernateEntityManagerFactory
     * hemf) { return hemf.getSessionFactory(); }
     */

    //
    // @Bean
    // public SessionFactory sessionFactory(@Qualifier("entityManagerFactory")
    // EntityManagerFactory emf) {
    // return emf.unwrap(SessionFactory.class);
    // }
    // @Autowired
    // private EntityManagerFactory entityManagerFactory;
    //
    //
    // @Bean
    // public SessionFactory getSessionFactory() {
    // if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
    // throw new NullPointerException("factory is not a hibernate factory");
    // }
    // return entityManagerFactory.unwrap(SessionFactory.class);
    // }
    //
    //
    // @Bean
    // public EntityManagerFactory entityManagerFactory() {
    // return null;
    // }
    @Bean
    public SessionFactory sessionFactory(EntityManagerFactory factory) {
        if (factory.unwrap(SessionFactory.class) == null) {
            throw new NullPointerException("factory is not a hibernate factory");
        }
        return factory.unwrap(SessionFactory.class);
    }
    //
    // @Bean
    // public HibernateJpaSessionFactoryBean sessionFactory() {
    // return new HibernateJpaSessionFactoryBean();
    // }
}
